﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HT.Data.BussinessLogic;
namespace Hotel.UserControls
{
    public partial class RoomType : System.Web.UI.UserControl
    {
        public int RoomTypeID
        {
            get;
            set;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            try
            {
                DataSet ds = HotelManger.GetInstance().GetRoomType(1, 0);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    gvpayment.DataSource = ds;
                    gvpayment.DataBind();
                }
                else
                {
                    DataTable dt = new DataTable();
                    dt.Columns.Add("RoomTypeId");
                    dt.Columns.Add("HotelId");
                    dt.Columns.Add("RoomType");
                    dt.Columns.Add("Occupancy");
                    dt.Columns.Add("ChargeNonAC");
                    dt.Columns.Add("ChargeAC");
                    dt.Columns.Add("IsExtraBedAllowed");
                    dt.Columns.Add("MaxExtraBedsAllowed");
                    dt.Columns.Add("ExtraBedChargeNonAC");
                    dt.Columns.Add("ExtraBedChargeAC");
                    dt.Columns.Add("CreatedBy");
                    dt.Columns.Add("CreatedDate");
                    dt.Columns.Add("ModifiedBy");
                    dt.Columns.Add("ModifiedDate");

                    DataRow dr = dt.NewRow();
                    dt.Rows.Add(dr);
                    gvpayment.DataSource = dt;
                    gvpayment.DataBind();
                    gvpayment.Rows[0].Visible = false;
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void LoadRoomTypeDetails(int id)
        {
            try
            {
                DataSet ds = HotelManger.GetInstance().GetRoomType(0, id);
                if (ds.Tables[0].Rows.Count > 0)
                {

                    RoomTypeTextBox.Text = ds.Tables[0].Rows[0]["RoomType"].ToString(); ;
                    OccupancyTextBox.Text = ds.Tables[0].Rows[0]["Occupancy"].ToString();
                    ChargeNonACTextBox.Text = ds.Tables[0].Rows[0]["ChargeNonAC"].ToString();
                    ChargeACTextBox.Text = ds.Tables[0].Rows[0]["ChargeNonAC"].ToString();
                    MaxExtraBedsAllowedTextBox.Text = ds.Tables[0].Rows[0]["MaxExtraBedsAllowed"].ToString();
                    ExtraBedChargeNonATextBox.Text = ds.Tables[0].Rows[0]["ExtraBedChargeNonAC"].ToString();
                    ExtraBedChargeACTextBox.Text = ds.Tables[0].Rows[0]["ExtraBedChargeAC"].ToString();
                    string[] splittax = ds.Tables[0].Rows[0]["Taxes"].ToString().Split(',');
                    string[] splitService = ds.Tables[0].Rows[0]["Services"].ToString().Split(',');

                    foreach (ListItem ls in checkBoxTax.Items)
                    {
                        for (int i = 0; i < splittax.Length; i++)
                        {
                            if (ls.Text ==  splittax[i].Trim())
                            {
                                ls.Selected = true;
                            }
                        }
                    }
                    foreach (ListItem lst in checkBoxService.Items)
                    {
                        for (int j = 0; j < splitService.Length; j++)
                        {
                            if (lst.Text == splitService[j].Trim())
                            {
                                lst.Selected = true;
                            }
                        }
                    }
                }


            }
            catch (Exception ex)
            {

            }
        }

        private void GetAllTax()
        {
            checkBoxTax.DataSource = HotelManger.GetInstance().GetAllTax(0);
            checkBoxTax.DataTextField = "TaxName";
            checkBoxTax.DataValueField = "TaxId";
            checkBoxTax.DataBind();
        }
        private void GetAllService()
        {
            checkBoxService.DataSource = HotelManger.GetInstance().GetAllRoomServices(0);
            checkBoxService.DataTextField = "ServiceName";
            checkBoxService.DataValueField = "ServiceId";
            checkBoxService.DataBind();
        }

        protected void gvpayment_RowEditing(object sender, GridViewEditEventArgs e)
        {
            // gvpayment.EditIndex = e.NewEditIndex;

            LoadData();
        }

        protected void gvpayment_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {

            gvpayment.EditIndex = -1;
            LoadData();
        }

        protected void gvpayment_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("Insert"))
            {
                if (!string.IsNullOrEmpty(((TextBox)gvpayment.FooterRow.FindControl("txtRoomType")).Text.Trim()))
                {
                    // rtype.RoomTypeId = Convert.ToInt32(gvpayment.DataKeys[e.RowIndex].Values[0].ToString());
                    HT.Data.Objects.RoomType rtype = new HT.Data.Objects.RoomType();
                    rtype.HotelId = 1;
                    rtype.RoomTypeName = ((TextBox)gvpayment.FooterRow.FindControl("txtRoomType")).Text.Trim();
                    rtype.Occupancy = Convert.ToInt32(((TextBox)gvpayment.FooterRow.FindControl("txtOccupancy")).Text.Trim());
                    rtype.ChargeNonAC = Convert.ToDouble(((TextBox)gvpayment.FooterRow.FindControl("txtNChargeAC")).Text.Trim());
                    rtype.ChargeAC = Convert.ToDouble(((TextBox)gvpayment.FooterRow.FindControl("txtChargeAC")).Text.Trim());
                    rtype.IsExtraBedAllowed = ((CheckBox)gvpayment.FooterRow.FindControl("chkservice")).Checked;
                    rtype.MaxExtraBedsAllowed = Convert.ToInt32(((TextBox)gvpayment.FooterRow.FindControl("txtMaxExtraBedsAllowed")).Text.Trim());
                    rtype.ExtraBedChargeNonAC = Convert.ToDouble(((TextBox)gvpayment.FooterRow.FindControl("txtExtraBedChargeNonA")).Text.Trim());
                    rtype.ExtraBedChargeAC = Convert.ToDouble(((TextBox)gvpayment.FooterRow.FindControl("txtExtraBedChargeAC")).Text.Trim());
                    rtype.ModifiedBy = 1;
                    HotelManger.GetInstance().InserteRoomType(rtype);
                    LoadData();
                }
            }

        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            try
            {
                HT.Data.Objects.RoomType rtype = new HT.Data.Objects.RoomType();
                rtype.HotelId = 0;
                rtype.RoomTypeName = RoomTypeTextBox.Text.Trim();
                rtype.Occupancy = Convert.ToInt32(OccupancyTextBox.Text.Trim());
                rtype.ChargeNonAC = Convert.ToDouble(ChargeNonACTextBox.Text.Trim());
                rtype.ChargeAC = Convert.ToDouble(ChargeACTextBox.Text.Trim());
                rtype.IsExtraBedAllowed = true;
                rtype.MaxExtraBedsAllowed = Convert.ToInt32(MaxExtraBedsAllowedTextBox.Text.Trim());
                rtype.ExtraBedChargeNonAC = Convert.ToDouble(ExtraBedChargeNonATextBox.Text.Trim());
                rtype.ExtraBedChargeAC = Convert.ToDouble(ExtraBedChargeACTextBox.Text.Trim());
                rtype.CreatedBy = 1;
                int rtypeid = 0;
                if (hdnMode.Value == "New")
                {
                    rtypeid = HotelManger.GetInstance().InserteRoomType(rtype);
                }
                else if (hdnMode.Value == "Edit")
                {
                    rtype.RoomTypeId = Convert.ToInt32(hdnRoomTypeID.Value);
                    rtype.ModifiedBy = 0;
                    HotelManger.GetInstance().UpdateRoomType(rtype);
                    rtypeid = rtype.RoomTypeId;
                }

                if (rtypeid > 0)
                {
                    HotelManger.GetInstance().RemoveRoomTypeTax(rtypeid);
                    foreach (ListItem item in checkBoxTax.Items)
                    {
                        if (item.Selected)
                        {
                            HotelManger.GetInstance().SaveRoomTypeTax(rtypeid, Convert.ToInt32(item.Value));
                        }
                    }
                    HotelManger.GetInstance().RemoveRoomTypeService(rtypeid);
                    foreach (ListItem items in checkBoxService.Items)
                    {
                        if (items.Selected)
                        {
                            HotelManger.GetInstance().SaveRoomTypeService(rtypeid, Convert.ToInt32(items.Value));
                        }
                    }
                }



                LoadData();
                pnlView.Visible = true;
                pnlAdd.Visible = false;
                ResetFormControlValues(this);
            }
            catch (Exception ex)
            {

            }
        }
        protected void gvpayment_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = Convert.ToInt32(gvpayment.DataKeys[e.RowIndex].Values[0].ToString());
            HotelManger.GetInstance().DeleteService(id);
            LoadData();
        }

        protected void gvpayment_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int id = Convert.ToInt32(gvpayment.DataKeys[e.RowIndex].Values[0].ToString());
            HT.Data.Objects.RoomType rtype = new HT.Data.Objects.RoomType();
            rtype.RoomTypeId = Convert.ToInt32(gvpayment.DataKeys[e.RowIndex].Values[0].ToString());
            rtype.HotelId = 1;
            rtype.RoomTypeName = ((TextBox)gvpayment.Rows[e.RowIndex].FindControl("txtRoomType")).Text.Trim();
            rtype.Occupancy = Convert.ToInt32(((TextBox)gvpayment.Rows[e.RowIndex].FindControl("txtOccupancy")).Text.Trim());
            rtype.ChargeNonAC = Convert.ToDouble(((TextBox)gvpayment.Rows[e.RowIndex].FindControl("txtNChargeAC")).Text.Trim());
            rtype.ChargeAC = Convert.ToDouble(((TextBox)gvpayment.Rows[e.RowIndex].FindControl("txtChargeAC")).Text.Trim());
            rtype.IsExtraBedAllowed = ((CheckBox)gvpayment.Rows[e.RowIndex].FindControl("chkservice")).Checked;
            rtype.MaxExtraBedsAllowed = Convert.ToInt32(((TextBox)gvpayment.Rows[e.RowIndex].FindControl("txtMaxExtraBedsAllowed")).Text.Trim());
            rtype.ExtraBedChargeNonAC = Convert.ToDouble(((TextBox)gvpayment.Rows[e.RowIndex].FindControl("txtExtraBedChargeNonA")).Text.Trim());
            rtype.ExtraBedChargeAC = Convert.ToDouble(((TextBox)gvpayment.Rows[e.RowIndex].FindControl("txtExtraBedChargeAC")).Text.Trim());
            rtype.ModifiedBy = 1;
            HotelManger.GetInstance().UpdateRoomType(rtype);
            gvpayment.EditIndex = -1;
            LoadData();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {

            hdnMode.Value = "New";
            lblMessage.Text = "Add Room Type";
            GetAllTax();
            pnlView.Visible = false;
            pnlAdd.Visible = true;
            GetAllService();


        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            pnlView.Visible = true;
            pnlAdd.Visible = false;
        }

        protected void lnkEdit_Command(object sender, CommandEventArgs e)
        {
            RoomTypeID = Convert.ToInt32(e.CommandName);
            hdnMode.Value = "Edit";
            lblMessage.Text = "Edit Room Type";
            hdnRoomTypeID.Value = RoomTypeID.ToString();
            pnlView.Visible = false;
            pnlAdd.Visible = true;
            GetAllTax();
            GetAllService();
            LoadRoomTypeDetails(RoomTypeID);

        }

        private void ResetFormControlValues(Control parent)
        {
            foreach (Control c in parent.Controls)
            {
                if (c.Controls.Count > 0)
                {
                    ResetFormControlValues(c);
                }
                else
                {
                    switch (c.GetType().ToString())
                    {
                        case "System.Web.UI.WebControls.TextBox":
                            ((TextBox)c).Text = "";
                            break;
                        case "System.Web.UI.WebControls.CheckBox":
                            ((CheckBox)c).Checked = false;
                            break;
                        case "System.Web.UI.WebControls.RadioButton":
                            ((RadioButton)c).Checked = false;
                            break;

                    }
                }
            }
        }
    }
}